// pages/cart/cart.js
import request from "../../utils/api"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopLists:[

    ],
    allCheck:false,
    totalPrice:0,
    sumNum:0
  },
  //
  toAddress(){
    wx.navigateTo({
      url: '/pages/address/address'
    })
  },
  onClickButton(){
    wx.navigateTo({
      url: '/pages/order/order'
    })
  },

  //删除业务
  deleteShop(e){
    let num = e.currentTarget.dataset.num;
    console.log(num)
    let newShop = this.data.shopLists;
    let index = newShop.findIndex(item=>item.id == num)
   
    console.log(newShop)
    wx.showModal({
      title: '提示',
      content:"确定删除吗？",
      success:(res)=>{   
        if(res.confirm){         
          if(newShop[index].check){
           newShop.splice(index,1)
           
           wx.setStorageSync('yougouProduct',newShop)
           this.getCartsData()
            // this.setData({
            //   shopLists:newShop
            // }) 
            this.getsum()
            wx.showToast({
              title: '删除成功',
            })
          }else{
            wx.showToast({
              title: '请选中后删除',
              icon:'error'
            })
          }
          
        }
      }
    })
  },

  //获取数据
  getCartsData(){
    const carts = wx.getStorageSync('yougouProduct');
    this.setData({
      shopLists:carts
    })
    console.log(carts)
  },

  //总价计算：
  getsum(){
    const priceSum = this.data.shopLists.filter(item=>item.check==true).reduce((pre,cur)=>{ return pre + cur.price*cur.num
    },0)
    const sumNum = this.data.shopLists.filter(item=>item.check==true).reduce((pre,cur)=>{ return pre + cur.num
    },0)
                  
    console.log(priceSum)
    this.setData({
      totalPrice:priceSum*100,
      sumNum
    })
  },



  //让步进器中的数据更新至data中
  onChangeNum(e){
    let  id = e.currentTarget.dataset.id;
    let num = e.detail;
    //声明一个变量接收data中的数据，因为dataset为异步
    let arrList = this.data.shopLists;
    console.log("gggggg",e,num)
    //let index = this.data.shopLists.findIndex(item=>item.id == id)
    let index = arrList.findIndex(item=>item.id == id)
    arrList[index].num = num;
    //this.data.shopLists[index].num = num
    wx.setStorageSync('yougouProduct', arrList)
      this.setData({
        shopLists:arrList
      })
     
      
    
    
    
    this.getsum()
    
  },
  geta(){
    this.setStorageSync('yougouProduct',this.data.shopLists)
  },
  



  //单选按钮
  onChange(e){
    //点击按钮进行状态改变
    let  id = e.currentTarget.dataset.id;
    //console.log(id)
    this.data.shopLists.forEach((item)=>{
      //console.log(item.id)
      if(item.id == id){
        item.check = !item.check
      }      
    })
    this.setData({
      shopLists:this.data.shopLists
    })
    //实现单选按钮控制全选按钮功能
    let flag = this.data.shopLists.every(item=>item.check)
    //console.log(flag)
    if(flag){
      this.setData({
        allCheck:true
      })
    }else{
      this.setData({
        allCheck:false
      })
    }
    this.getsum()
    wx.setStorageSync('yougouProduct',this.data.shopLists)
  },
  //全选按钮
  onAllChange(){
    //实现全选按钮的切换
    this.setData({
      allCheck:!this.data.allCheck,
      check:!this.data.allCheck
    })

    this.data.shopLists.forEach((item)=>{
      item.check= this.data.allCheck
    })
    this.setData({
      shopLists:this.data.shopLists
    })
    this.getsum()
    wx.setStorageSync('yougouProduct',this.data.shopLists)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCartsData()
    this.getsum()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCartsData()
    //this.data.shopLists.map(item=>item.check = false)
    let flag =  this.data.shopLists.every(item=>item.check == true)
    this.setData({
      shopLists:this.data.shopLists,
      allCheck:flag
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})